package com.yanqu.road.dao.impl.activity.xingyoushanshui.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiTeamData;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXingYouShanShuiTeamDataDaoImpl extends TempDataObjectDao<CrossXingYouShanShuiTeamData> {

    public static String getCrossXingYouShanShuiTeamDataSql = "select * from `t_c_xing_you_team_data` where `activityId`=? and groupId = ?";
    public Map<Integer, CrossXingYouShanShuiTeamData> getCrossXingYouShanShuiTeamData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, CrossXingYouShanShuiTeamData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossXingYouShanShuiTeamDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXingYouShanShuiTeamData data = parse(rs);
                result.put(data.getTeamId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossXingYouShanShuiTeamDataSql = "update `t_c_xing_you_team_data` set silverCoin=?,silverCoinTotal=?,silverUpdateTime=?,todayTeam=?,randomTodayTeam=?,visitorMap=?,useEnergy=?,masterId=?," +
            "wishValue=?,bigWishTimes=?,wishRound=?,captorMap=?,captorReward=?,declaration=? where activityId=? and groupId = ? and teamId=?";
    @Override
    protected boolean updateData(CrossXingYouShanShuiTeamData crossXingYouShanShuiTeamData) {
        crossXingYouShanShuiTeamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamData.getSilverCoin()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamData.getSilverCoinTotal()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamData.getSilverUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiTeamData.getTodayTeam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiTeamData.getRandomTodayTeam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiTeamData.getVisitorMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getUseEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamData.getMasterId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getWishValue()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getBigWishTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getWishRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiTeamData.getCaptorList())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(crossXingYouShanShuiTeamData.getCaptorReward())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossXingYouShanShuiTeamData.getDeclaration()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getTeamId()));
        if (getDb().executeNoneQuery(updateCrossXingYouShanShuiTeamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossXingYouShanShuiTeamDataSql = "insert into `t_c_xing_you_team_data`(activityId,groupId,teamId,silverCoin,silverCoinTotal,silverUpdateTime,todayTeam,randomTodayTeam,visitorMap,useEnergy,masterId," +
            "wishValue,bigWishTimes,wishRound,captorMap,captorReward,declaration) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossXingYouShanShuiTeamData crossXingYouShanShuiTeamData) {
        crossXingYouShanShuiTeamData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamData.getSilverCoin()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamData.getSilverCoinTotal()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamData.getSilverUpdateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiTeamData.getTodayTeam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiTeamData.getRandomTodayTeam())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiTeamData.getVisitorMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getUseEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, crossXingYouShanShuiTeamData.getMasterId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getWishValue()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getBigWishTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, crossXingYouShanShuiTeamData.getWishRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossXingYouShanShuiTeamData.getCaptorList())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(crossXingYouShanShuiTeamData.getCaptorReward())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossXingYouShanShuiTeamData.getDeclaration()));
        if (getDb().executeNoneQuery(insertCrossXingYouShanShuiTeamDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossXingYouShanShuiTeamData parseData(ResultSet rs) throws SQLException {
        CrossXingYouShanShuiTeamData crossXingYouShanShuiTeamData = new CrossXingYouShanShuiTeamData();
        crossXingYouShanShuiTeamData.setActivityId(rs.getInt("activityId"));
        crossXingYouShanShuiTeamData.setTeamId(rs.getInt("teamId"));
        crossXingYouShanShuiTeamData.setSilverCoin(rs.getLong("silverCoin"));
        crossXingYouShanShuiTeamData.setSilverCoinTotal(rs.getLong("silverCoinTotal"));
        crossXingYouShanShuiTeamData.setSilverUpdateTime(rs.getLong("silverUpdateTime"));
        crossXingYouShanShuiTeamData.setTodayTeam(JSON.parseObject(rs.getString("todayTeam"), new TypeReference<List<Integer>>() {}));
        crossXingYouShanShuiTeamData.setRandomTodayTeam(JSON.parseObject(rs.getString("randomTodayTeam"), new TypeReference<List<Integer>>() {}));
        crossXingYouShanShuiTeamData.setVisitorMap(JSON.parseObject(rs.getString("visitorMap"), new TypeReference<ConcurrentHashMap<Long, Long>>() {}));
        crossXingYouShanShuiTeamData.setUseEnergy(rs.getInt("useEnergy"));
        crossXingYouShanShuiTeamData.setMasterId(rs.getLong("masterId"));
        crossXingYouShanShuiTeamData.setWishValue(rs.getInt("wishValue"));
        crossXingYouShanShuiTeamData.setBigWishTimes(rs.getInt("bigWishTimes"));
        crossXingYouShanShuiTeamData.setWishRound(rs.getInt("wishRound"));
        crossXingYouShanShuiTeamData.setCaptorList(JSON.parseObject(rs.getString("captorMap"), new TypeReference<List<Integer>>() {}));
        Property captorReward = PropertyHelper.parseStringToProperty(rs.getString("captorReward"));
        if(captorReward == null){
            captorReward = new Property();
        }
        crossXingYouShanShuiTeamData.setCaptorReward(captorReward);
        crossXingYouShanShuiTeamData.setDeclaration(rs.getString("declaration"));
        crossXingYouShanShuiTeamData.setGroupId(rs.getInt("groupId"));

        return crossXingYouShanShuiTeamData;
    }
}
